package com.zfzs.post.modules.agent.service.impl;

import com.zfzs.post.modules.agent.domain.PolicyDivided;
import com.zfzs.post.modules.agent.repository.PolicyDividedRepository;
import com.zfzs.post.modules.agent.service.PolicyDividedService;
import com.zfzs.post.modules.agent.service.dto.PolicyDividedQueryCriteria;
import com.zfzs.post.modules.agent.service.mapper.PolicyDividedMapper;
import com.zfzs.post.utils.QueryHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Optional;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
 * @author ck
 * @date 2019-11-27
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PolicyDividedServiceImpl implements PolicyDividedService {

    @Autowired
    private PolicyDividedRepository policyDividedRepository;

    @Autowired
    private PolicyDividedMapper policyDividedMapper;

    @Override
    public Object queryAll(PolicyDividedQueryCriteria criteria, Pageable pageable) {
        Page<PolicyDivided> page = policyDividedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return page;
    }

    @Override
    public Object queryAll(PolicyDividedQueryCriteria criteria) {
        return policyDividedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
    }

    @Override
    public PolicyDivided findById(Long id) {
        Optional<PolicyDivided> policyDivided = policyDividedRepository.findById(id);
        return policyDivided.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PolicyDivided create(PolicyDivided resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return policyDividedRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PolicyDivided resources) {
        Optional<PolicyDivided> optionalPolicyDivided = policyDividedRepository.findById(resources.getId());
        PolicyDivided policyDivided = optionalPolicyDivided.get();
        policyDivided.copy(resources);
        policyDividedRepository.save(policyDivided);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        policyDividedRepository.deleteById(id);
    }

    @Override
    public BigDecimal getPolicyAmount(Long agentId, Long terminalId, Long channelId, String names) {
        PolicyDivided policyDivided = policyDividedRepository.findPolicyDivided(agentId, terminalId, channelId, names);
        if (policyDivided == null) {
            return new BigDecimal("0.00");
        }
        return policyDivided.getAmount();
    }

    @Override
    public PolicyDivided getPolicyInfo(Long agentId, Long terminalId, Long channelId, String names) {
        PolicyDivided policyDivided = policyDividedRepository.findPolicyDivided(agentId, terminalId, channelId, names);
        if (policyDivided == null) {
            return null;
        }
        return policyDivided;
    }

    @Override
    public void addPolicyDivided(Long agentId, Long terminalId, Long channelId, BigDecimal amount, String names) {
        PolicyDivided p = new PolicyDivided();
        p.setAgentId(agentId);
        p.setAmount(amount);
        p.setChannelId(channelId);
        p.setTerminalId(terminalId);
        p.setNames(names);
        create(p);
    }
}